import Vue from 'vue'
import Router from 'vue-router'

import Login from '@/views/admin/LoginView'
import Main from '@/views/admin/Main'
import UserInfo from '@/views/admin/UserInfo'
import EmpInfo from '@/views/admin/Empinfo'
import RoleManagement from '@/views/admin/RoleManagement'
import Area from '@/views/admin/Area'
import Spot from '@/views/admin/Spot'
import Log from '@/views/admin/Log'
import Order from '@/views/admin/Order'
import Echarts from '@/views/admin/Echarts'

Vue.use(Router)

//创建一个路由
const routes = [
  { path: '/', name: 'login', component: Login },
  {
    path: '/main', component: Main,
    children: [
      {path:'/', component: Echarts },
      {path:'/userinfo', component: UserInfo },
      { path: '/empinfo', component: EmpInfo },
      { path: '/role', component: RoleManagement },
      { path: '/order', component: Order },
      { path: '/area', component: Area },
      { path: '/spot', component: Spot },
      { path: '/log', component: Log },
      { path: '/echarts', component: Echarts },
    ]
  },
]

//创建一个路由对象，命名为router
const router = new Router({
  routes: routes//使用路由数组初始化路由
})

router.beforeEach((to, from, next) => {
  //alert(to.name+""+to.path);
  if (to.path == '/' || to.path == 'regist') {
    next();//对登录页和注册页放行
  } if (localStorage.getItem("token") == '' || localStorage.getItem("token") == null) {
    //alert(localStorage.getItem("userName"));
    next('/');//去登录
  } else {
    next();//继续路由
  }

})



//暴露路由对象
export default router


